package com.lagou.common;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

/**
 * 存放每次请求耗时
 * @Author: zhangzhiwei
 * @Date: 2022/3/13 17:54
 */
public class TPMonitorContainer {

    private static final Map<String, Queue<RequestNode>> map = new HashMap<>();

    public static void offer(String key, RequestNode tailNode){

        synchronized (TPMonitorContainer.class){
            Queue<RequestNode> queue = map.get(key);
            if(queue == null){
                queue = new LinkedList<>();
            }
            queue.offer(tailNode);
            // 判断队头和队尾元素的差是否超过一分钟，超过则移除队头元素
            /*Long requestTime = tailNode.getRequestTime();
            while (requestTime - queue.peek().getRequestTime() > 60 * 1000) {
                queue.poll();
            }*/
            map.put(key, queue);
        }
    }

    public static Map<String, Queue<RequestNode>> getMap(){
        return map;
    }

    public static Queue<RequestNode> getQueue(String key){
        return map.get(key);
    }
}
